package org.jeecg.modules.kc.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.kc.entity.KcDepositoryInGoods;
import org.jeecg.modules.kc.enums.StorageEnum;
import org.jeecg.modules.kc.mapper.KcDepositoryInGoodsMapper;
import org.jeecg.modules.kc.service.IKcDepositoryInGoodsService;
import org.jeecg.modules.kc.vo.GoodStorage;
import org.jeecg.modules.rooms.entity.CesGoods;
import org.jeecg.modules.rooms.service.CesGoodsServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: kc_depository_in_goods
 * @Author: jeecg-boot
 * @Date:   2023-04-18
 * @Version: V1.0
 */
@Service
public class KcDepositoryInGoodsServiceImpl extends ServiceImpl<KcDepositoryInGoodsMapper, KcDepositoryInGoods> implements IKcDepositoryInGoodsService {


    /**
     * 仓库库存商品列表
     */
    public Page<KcDepositoryInGoods> depositoryInGoodsList(Page<KcDepositoryInGoods> page, KcDepositoryInGoods kcDepositoryInGoods){
        return page.setRecords(baseMapper.depositoryInGoodsList(page,kcDepositoryInGoods.getHotelId(),
                kcDepositoryInGoods.getGoodsName(), kcDepositoryInGoods.getDepositoryId(),kcDepositoryInGoods.getGoodsId()));
    }

    @Override
    public IPage<GoodStorage> storagePage(Page<GoodStorage> page, GoodStorage goodStorage) {
        IPage<GoodStorage> storagePage = baseMapper.storagePage(page, goodStorage);
        if (ObjectUtil.equals(goodStorage.getWarningState(),1)) {
            List<GoodStorage> list = storagePage.getRecords().stream().filter(e -> e.getNum() > e.getStockWarning()).collect(Collectors.toList());
            storagePage.setRecords(list);
        } else if(ObjectUtil.equals(goodStorage.getWarningState(),2)){
            List<GoodStorage> list = storagePage.getRecords().stream().filter(e -> e.getNum() < e.getStockWarning()).collect(Collectors.toList());
            storagePage.setRecords(list);
        }
        return storagePage;
    }

    @Override
    public IPage<GoodStorage> storageDetailPage(Page<GoodStorage> page, GoodStorage goodStorage) {
        IPage<GoodStorage> storagePage = baseMapper.storageDetailPage(page, goodStorage);
        storagePage.getRecords().forEach(e ->{
            if (StrUtil.isNotEmpty(e.getStockType())){
                e.setStockType(Objects.requireNonNull(StorageEnum.val(Integer.valueOf(e.getStockType()))).getTitle());
            }
        });
        return storagePage;
    }

    /**添加销售出库*/
    @Override
    public void addSell(KcDepositoryInGoods depositoryInGoods) {
        LoginUser user = TokenUtils.getAuthUser();
        LambdaQueryWrapper<KcDepositoryInGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KcDepositoryInGoods::getGoodsId,depositoryInGoods.getGoodsId());
        queryWrapper.eq(KcDepositoryInGoods::getHotelId,depositoryInGoods.getHotelId());
        List<KcDepositoryInGoods> depositoryInGoodsList = list(queryWrapper);
        if (depositoryInGoodsList.size() == 0){
            throw new JeecgBootException("商品在库存中已不足");
        }
        //仓库可能为空
        depositoryInGoodsList.forEach(e -> {
            if (StrUtil.isEmpty(e.getDepositoryId())){
                e.setDepositoryId("无");
            }
        });
        Map<String, List<KcDepositoryInGoods>> map = depositoryInGoodsList.stream().collect(Collectors.groupingBy(KcDepositoryInGoods::getDepositoryId));
        // 按仓库分组，如果这个仓库商品没有了，就查下一个仓库
        for (Map.Entry<String, List<KcDepositoryInGoods>> entry:map.entrySet()){
            int sum = entry.getValue().stream().mapToInt(KcDepositoryInGoods::getNum).sum();
            if (sum > depositoryInGoods.getNum()){
                depositoryInGoods.setNum(-depositoryInGoods.getNum());
                depositoryInGoods.setDepositoryId(entry.getKey());
                depositoryInGoods.setTenantId(user.getRelTenantIds());
                break;
            }
        }
//        if (depositoryInGoods.getDepositoryId() == null){
//            throw new JeecgBootException("商品在库存中已不足");
//        }
        save(depositoryInGoods);
    }

    /**分页查询销售详情表*/
    @Override
    public IPage<GoodStorage> goodsSellDetailPage(Page<GoodStorage> page, GoodStorage goodStorage) {
        IPage<GoodStorage> storagePage = baseMapper.goodsSellDetailPage(page, goodStorage);
        return storagePage;
    }

    /**分页查询销售汇总表*/
    @Override
    public IPage<GoodStorage> goodsSellCollect(Page<GoodStorage> page, GoodStorage goodStorage) {
        IPage<GoodStorage> storagePage = baseMapper.goodsSellCollect(page, goodStorage);
        return storagePage;
    }
}
